﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rimz.Common;
using System.Transactions;

namespace Rimz.DataAccessLayer
{
    public partial class RimzDataContext : IDataContext
    {
        public Family GetFamilyByID(int familyID)
        { 
            return Families.FirstOrDefault(f => f.FamilyID == familyID);
        }

        public User GetUserByID(int userID)
        {
            return Users.FirstOrDefault(u => u.UserID == userID);
        }

        public int CreateNewUser(string lastName, string firstName, string email, string password)
        {
            bool isValidInput = Rimz.Common.Utilities.isValidEmail(email) &&
                                Rimz.Common.Utilities.isValidText(lastName) &&
                                Rimz.Common.Utilities.isValidText(firstName) &&
                                Rimz.Common.Utilities.isValidPassword(password);
            
            using (var ts = new TransactionScope())
            {
                User user;

                if (isValidInput)
                {
                    user = Users.FirstOrDefault(u => u.Email == email);

                    if (user != null) // existing users
                    {
                        throw new InvalidOperationException("User's email already exists");
                    }

                    var now = DateTime.UtcNow;

                    // TODO: Fix saving passwords as plain text.  Add MD5 and salt the password.

                    user = new User
                                    {
                                        FirstName = firstName,
                                        LastName = lastName,
                                        Email = email,
                                        Password = password,
                                        CreateDate = now
                                    };

                    Users.InsertOnSubmit(user);
                    SubmitChanges();

                }
                else
                {
                    throw new InvalidOperationException("User's input data is not valid");
                }

                ts.Complete();

                return user.UserID;
                
            }
                    
        }

        public int CreateNewFamily(string familyName, string familyDescription)
        {
            bool isValidInput = Rimz.Common.Utilities.isValidText(familyName) &&
                                Rimz.Common.Utilities.isValidText(familyDescription);

            using (var ts = new TransactionScope())
            {
                Family family;

                if (isValidInput)
                {
                    var now = DateTime.UtcNow;

                    family = new Family
                                        {
                                            FamilyName = familyName,
                                            FamilyDescription = familyDescription,
                                            CreateDate = now
                                        };

                }
                else
                {
                    throw new InvalidOperationException("User's input data is not valid.");
                }

                return family.FamilyID;
            }
                        
        }



    }
}
